var inputVal = getQueryString2('inputVal');

function getQueryString2(name) {
	
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
    	var parmsVal = decodeURI(r[2]);
        return unescape(parmsVal);
    }  	 
    return null;
 }
var nowing='1';//1可以执行  2 执行中
var page=1;//定位当前页数
var index = 1;
var forEnd =0;
var showPage=10;//初始化最多显示的页数
var showPageCount=7;//初始化每页显示的条数

searchAll(inputVal,1,showPageCount);
function search2(){
	var text = $("#textId").val().replace(/\s/g, "");
	if(text==''){
		return;
	}
	if(nowing=='2'){
		alert("正在查询...");
		return;
	}
	page=1;//定位当前页数
	index = 1;
	forEnd =0;
	searchAll(text,1,showPageCount);
}
	//全文检索
function searchAll(inputVal,page,count){
	if(nowing=='2'){
		alert("正在查询...");
		return;
	}
	nowing='2';
	inputVal=decodeURI(inputVal);
	inputVal = encodeURI(encodeURI(inputVal));
	$.ajax({
		  type: "post",
		  url: "../ck/queryCkHtml",
		  data: {
			  'inputVal':inputVal,
			  'sort':'yes',
			  'state':'1',
			  'listCktype':'5,6,7,8,9,10,11,12,13,21,22,23,26,27,30,31,32',
			  'page':page,
			  'rows':count
			  },
		  dataType: "json",
		  error:function(){
			  nowing='1';
		  },
		  success: function(data){
			  $("#msgDivId").empty();
			  $("#pagnub").empty();
			 
			  var rows = data.rows;
			  var total = data.total;

			  $.each(rows, function(index, entity){
				  var htmltype = '0';
				  var ckType = entity.ckType;
				  if(ckType==17||ckType==18||ckType==19||
						  ckType==20||ckType==24||ckType==25){
					  return true;
				  }
				  var htmlId = entity.id;
				  var title = entity.title;
				  var html = entity.html;
		
				  var nowDate = entity.createTime;
				  var now = new Date(nowDate);
				  var time= now.format("yyyy-MM-dd");
				  if(title=='CKEditorHtml'){
					  htmltype = '1';
					  title=entity.summary;
				  }
				  
				  if(html.length>500){
					  html = html.substring(0,300); 
					  html+='......';
				  }
				  inputVal = decodeURI(decodeURI(inputVal));
				  var title2 = title.replace(inputVal,"<span style='color:red'>"+inputVal+"</span>") ;
				
				  var html2 =  html.replace(inputVal,"<span style='color:red'>"+inputVal+"</span>") ; 
				 
				  $.ajax({
					  type: "post",
					  url: "../menu/queryMsgByCktype",
					  data: {
						  'ckType':ckType
						  },
					  dataType: "json",
					  error:function(){
						  nowing='1';
					  },
					  success: function(data){
						  var lsEntity = data.lsEntity;
						  if(lsEntity.length>0){
							  var menu = lsEntity[0];
							  var qianUrl = menu.qianUrl;
							  if(htmltype=='0'){
								  qianUrl+='&data='+htmlId;
							  }
							
							  var msgStr='<p id="newstitle"><a href="'
								  +qianUrl+'">'
								  +'<span class="title">'
								  +title2+'</span></a><span class="nub">'
								  +time+'</span></p><a href="'
								  +qianUrl+'">'
								  +html2+'<br /></a>';
							  
							  $("#msgDivId").append(msgStr);
						  }
					  }
				  })
			  })
			 
			  var totalStr = fenye(inputVal,page,total);
			  $("#pagnub").append(totalStr);
			  nowing='1';
		  }
	})
}

//分页
function fenye(inputVal,page,total){
	
	var pageAll = Math.ceil(total/showPageCount);//总页数
	 
	  if(page==1){
		  forEnd = pageAll;
		  if(forEnd>=showPage){
			  forEnd=showPage;
		  }
	  }
	  
	  var totalStr1='<a href="javascript:loadBegin('+"'"+inputVal+"'"+');">'
	  	  +'<span class="top"></span></a>'
	      +'<a href="javascript:loadUp('+"'"+inputVal+"'"+','+page+');">'
	      +'<span class="arrowleft"></span></a>';
	  var totalStr2='';
	 
	  for (var i = index; i <=forEnd; i++) {
		if(page==i){
			totalStr2+='<a  id="aid'+i+'"' 
			+'href="javascript:loadIndex('+"'"+inputVal+"'"+','+i+');"><b>'+i+'</b></a>';
		}else{
			totalStr2+='<a id="aid'+i+'"' 
			+'href="javascript:loadIndex('+"'"+inputVal+"'"+','+i+');"><span>'+i+'</span></a>';
		}
	  }
	  if(forEnd<pageAll){
		  totalStr2+=' ......';
	  }
	 var totalStr = totalStr1+totalStr2;
	  totalStr+='<a href="javascript:loadNext('+"'"+inputVal+"'"+','+page+','+pageAll+');">'
	      +'<span class="arrowrightactive"></span></a>'
	      +'<a href="javascript:loadEnd('+"'"+inputVal+"'"+','+pageAll+');">'
	      +'<span class="bottomactive"></span></a>';
	 return totalStr;
}


//下一页
function loadNext(inputVal,page,pageAll){
	if(nowing=='2'){
		alert("正在查询...");
		return;
	}
	if(page>=pageAll){
		page=pageAll;
		return;
	}
	
	if(page%showPage==0){
		  forEnd=page+showPage;
		  if(forEnd>=pageAll){
			  forEnd=pageAll;
		  }
		  index =page+1;   
	 }
	page=page+1;
	searchAll(inputVal,page,showPageCount);	
}
//最后一页
function loadEnd(inputVal,pageAll){

	if(nowing=='2'){
		alert("正在查询...");
		return;
	}
	
	if(pageAll==0){
		return;
	}
	page=pageAll;
	forEnd=page;
	if(page%showPage!=0){
		index =page-(page%showPage)+1;
	}else{
		index =page-(showPage-1);
	}
	if(index<=1){
		index=1;
	}
	searchAll(inputVal,page,showPageCount);	
}
//上一页
function loadUp(inputVal,page){
	if(nowing=='2'){
		alert("正在查询...");
		return;
	}
	if(page<=1){
		page=1;
		return;
	}
	if((page-1)%showPage==0){
		  index =page-showPage; 
		  forEnd=page-1;
	 }
	page=page-1;
	searchAll(inputVal,page,showPageCount);	
}
//首页
function loadBegin(inputVal){
	if(nowing=='2'){
		alert("正在查询...");
		return;
	}
	page=1;
	forEnd=showPage;
	index =1; 
	searchAll(inputVal,page,showPageCount);	

}
//点击数字跳转到具体的页数
function loadIndex(inputVal,p){
	if(nowing=='2'){
		alert("正在查询...");
		return;
	}
	page=p;
	searchAll(inputVal,page,showPageCount);	
}
Date.prototype.format = function (format) {  
    var o = {  
        "M+": this.getMonth() + 1, // month  
        "d+": this.getDate(), // day  
        "h+": this.getHours(), // hour  
        "m+": this.getMinutes(), // minute  
        "s+": this.getSeconds(), // second  
        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter  
        "S": this.getMilliseconds()  
        // millisecond  
    }  
    if (/(y+)/.test(format))  
        format = format.replace(RegExp.$1, (this.getFullYear() + "")  
            .substr(4 - RegExp.$1.length));  
    for (var k in o)  
        if (new RegExp("(" + k + ")").test(format))  
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));  
    return format;  
} 